#!/bin/bash
REMOTEIP=""
Usage(){
	echo "`basename $0` <domain file> <output dir>"
	exit 1
}

if [ $# -lt 2 ]
then
	Usage
fi
DOMAIN_FILE=$1
DOMAIN_DIR=$2

ResolvIpByDns(){
	TMPDOMAIN=`dig mx $1|grep -1 "ANSWER SECTION"|grep MX|awk {'print $6'}`
	if [ "x"$TMPDOMAIN != "x" ]
	then 
		REMOTEIP=`basename $TMPDOMAIN .`
		return
	fi
	TMPDOMAIN=`dig A $1|grep -4 "ANSWER SECTION"|grep -E "\bA\b"|awk {'print $5'}|grep -v -E "^\s*$"`
	if [ "x""$TMPDOMAIN" != "x" ]
	then 
		REMOTEIP=$TMPDOMAIN
	fi
}

while read line
do
	ResolvIpByDns $line
	echo "$line $REMOTEIP" >> $DOMAIN_DIR/`basename $DOMAIN_FILE`.resolv
	REMOTEIP=""
done < $DOMAIN_FILE
